package com.stms.service.impl;

import com.stms.dao.SendClientRepository;
import com.stms.pojo.SendClient;
import com.stms.service.SendClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SendClientServiceImpl implements SendClientService {
    @Autowired
    private SendClientRepository sendClientRepository;

    @Override
    public boolean addSendClient(SendClient client) {
        if(sendClientRepository.findByIdcard(client.getIdcard())!=null) {
            return false;
        }else{
            sendClientRepository.save(client);
            return true;
        }
    }

    @Override
    public boolean editSendClient(SendClient client) {
        if(sendClientRepository.save(client)!=null){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public void deleteBatch(String clientIds) {
        String[] clientIdsArr = clientIds.split(",");
        List<Integer> list = new ArrayList<Integer>();

        for(String str:clientIdsArr){
            list.add(Integer.valueOf(str));
        }
        sendClientRepository.deleteBatch(list);
    }

    @Override
    public SendClient findByName(String client_name) {
        return sendClientRepository.findByName(client_name);
    }

    @Override
    public Page<SendClient> fuzzyQuery(Integer pageNum, Integer pageSize, String direction, SendClient client) {
        if(pageNum == null||pageNum==0){
            pageNum=1;
        }
        pageNum = pageNum-1;
        if (pageSize == null){
            pageSize=10;
        }
        if (direction == null){
            direction = new String("DESC");
        }

        Sort.Direction direc = direction.equals("DESC")?Sort.Direction.DESC:Sort.Direction.ASC;
        Pageable pageable = PageRequest.of(pageNum,pageSize,direc,"id");

        Page<SendClient> page = sendClientRepository.findAll(new Specification<SendClient>(){
            @Override
            public Predicate toPredicate(Root<SendClient> root, CriteriaQuery<?> query, CriteriaBuilder cb){
                List<Predicate> list =new ArrayList<>();
                if(client == null){
                    return null;
                }
                if(client.getId()!=null && !client.getId().equals("")){
                    list.add(cb.equal(root.get("id"),client.getId()));
                }
                if(client.getName()!=null && !client.getName().equals("")){
                    list.add(cb.like(root.get("name").as(String.class),"%"+client.getName().trim()+"%"));
                }
                if(client.getPhoneNum()!=null && !client.getPhoneNum().equals("")){
                    list.add(cb.like(root.get("phoneNum").as(String.class),"%"+client.getPhoneNum().trim()+"%"));
                }
                if(client.getIdcard()!=null && !client.getIdcard().equals("")){
                    list.add(cb.like(root.get("idcard").as(String.class),"%"+client.getIdcard().trim()+"%"));
                }
                if(client.getAddress()!=null && !client.getAddress().equals("")){
                    list.add(cb.like(root.get("address").as(String.class),"%"+client.getAddress().trim()+"%"));
                }
                Predicate[] predicates = new Predicate[list.size()];
                return cb.and(list.toArray(predicates));
            }
        },pageable);
        return page;
    }



    @Override
    public Page<SendClient> findByPage(Integer pageNum, Integer pageSize){
        if(pageNum==null||pageNum==0){
            pageNum=1;
        }
        pageNum=pageNum-1;
        if(pageSize==null||pageSize==0){
            pageSize=10;
        }

        PageRequest of = PageRequest.of(pageNum,pageSize);
        Page<SendClient> s = sendClientRepository.findAll(of);
        return s;
    }
}
